package com.onetech.adrewards.free.models;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;

import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.config.AppConfig.URLRequestApi;
import com.onetech.adrewards.free.http.APIRequestResult;
import com.onetech.adrewards.free.http.ZuZuHttpRequest;
import com.onetech.adrewards.free.listener.GifCardIF;
import com.onetech.adrewards.free.objects.ExchangeDTO;
import com.onetech.adrewards.free.objects.ExchangeGiftCarInfoDTO;
import com.onetech.adrewards.free.objects.GiftCardDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;

public class GiftCardModel implements GifCardIF{

	private static GifCardIF _instance;
	private static final Lock createLock=new ReentrantLock();
	
	public GiftCardModel(){
		
	}
	public static GifCardIF getInstance(){
		if(_instance==null){
			createLock.lock();
			if(_instance==null){
				_instance=new GiftCardModel();
			}
			createLock.unlock();
		}
		return _instance;
	}
	
	@Override
	public List<ExchangeDTO> getListHistoryUserExchanged(String uId,String pageNum) {
		Bundle params=new Bundle();
		APIRequestResult result=ZuZuHttpRequest.getInstance().getDataFromServer(URLRequestApi.GET_USER_EXCHANGED+uId+"/"+pageNum, params);
		return getListGiftCardUserExchangedFromAPIRequestResult(result);
	}
	private List<ExchangeDTO> getListGiftCardUserExchangedFromAPIRequestResult(APIRequestResult data){
		try {
			List<ExchangeDTO> result=null;
			
			if (data.error_code.trim().equals("0") && data.data != null && !data.data.equals("")) {
				JSONObject jsonObject=(JSONObject)data.data;
				JSONArray jArr=jsonObject.getJSONArray("result");
				result=new ArrayList<ExchangeDTO>();
				if (jArr != null && jArr.length() >= 0) {					
					for (int i = 0; i < jArr.length(); i++) {
						JSONObject jb=jArr.getJSONObject(i);
						ExchangeDTO item=parserExchange(jb);
						if(item!=null){
							result.add(item);
						}
					}
					
				}else{
					return result;
				}
			}
			return result;
		} catch (Exception e) {
			return null;
		}
	}

	private ExchangeDTO parserExchange(JSONObject data){
		try {
			ExchangeDTO result=new ExchangeDTO();
			String dateTime=data.getString("created_at");			
			result.setCreated_at(dateTime);
			result.setGift_card_code(data.getString("gift_card_code"));
			result.setGift_card_id(data.getString("id"));
			result.setPrice(data.getString("price"));
			result.setUseCoin(data.getString("use_coin"));
			result.setType(data.getString("type"));
			result.setImg(data.getString("img"));
			String expire = "";
			if(!data.isNull("expire_date"))
			{
				expire = "有効期限：" + data.getString("expire_date");
			}
			
			if(!data.isNull("csc"))
			{
				expire += "(CSC:" + data.getString("csc") + ")";
			}
			
			result.setExpireDate(expire);
			
			
			return result;
		} catch (Exception e) {
			return null;
		}
	}
	@Override
	public APIRequestResult exchangeProcess(Bundle params) {
		return ZuZuHttpRequest.getInstance().postDataToServer(URLRequestApi.GET_EXCHANGE_PROCESS, params);
	}
	@Override
	public ExchangeGiftCarInfoDTO getExchangeGiftCardInfo() {
		Bundle params=new Bundle();
		AdrewadsUtils.checkUId();
		APIRequestResult result=ZuZuHttpRequest.getInstance().getDataFromServer(URLRequestApi.GET_EXCHANGED_GIFT_CARD_INFO+AppConfig.uId, params);
		return getExchangeInfoFromAPIRequestResult(result);
	}
	private ExchangeGiftCarInfoDTO getExchangeInfoFromAPIRequestResult(APIRequestResult data){
		try {
			ExchangeGiftCarInfoDTO result=null;
			
			if (data.error_code.trim().equals("0") && data.data != null && !data.data.equals("")) {
				
				JSONObject jsonObject=(JSONObject)data.data;
				result=new ExchangeGiftCarInfoDTO();
				result.setMem_id(jsonObject.getString("mem_id"));
				result.setToken(jsonObject.getString("token"));
				JSONObject jb=jsonObject.getJSONObject("result");
				result.setTotal_coin(jb.getInt("total_coin"));
				JSONArray jArr=jb.getJSONArray("cards");
				if(jArr!=null && jArr.length()>0){
					List<GiftCardDTO> giftCard=new ArrayList<GiftCardDTO>();
					for (int i = 0; i < jArr.length(); i++) {
						JSONObject temp=jArr.getJSONObject(i);
						GiftCardDTO gif=new GiftCardDTO();
						gif.setCard_type(temp.getInt("card_type"));
						gif.setImg(temp.getString("img"));
						gif.setName(temp.optString("text"));
						giftCard.add(gif);
					}
					result.setCards(giftCard);
				}
				
			}
			return result;
		} catch (Exception e) {
			return null;
		}
	}
}
